package com.sky.com.sky.web.app;

import com.sky.constant.StatusConstant;
import com.sky.dto.DishPageDTO;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user/dish")
@Slf4j
@RequiredArgsConstructor
public class AppDishController {
    @Autowired
    DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    //根据分类id查询菜品列表
//    @GetMapping("/list")
//    public Result getListByCategoryId(Long categoryId){
//        DishPageDTO dishPageDTO = DishPageDTO.builder().categoryId(categoryId)
//                .status(StatusConstant.ENABLE)
//                .build();
//        // 调用service查询,返回结果
//        return Result.success(dishService.getParamList(dishPageDTO));
//    }

    //根据分类id查询菜品列表
    @GetMapping("/list")
    public Result getListByCategoryId(Long categoryId){
        ValueOperations valueOperations = redisTemplate.opsForValue();

        //1. 先从reis服务器钟获取集合数据
        List<DishVO> dishVOList = (List<DishVO>) valueOperations.get("DISH:" + categoryId);
        //2. 判断集合是否为null
        if(dishVOList != null){
            log.info("从reids服务器钟获取的数据:{}",dishVOList);
            return Result.success(dishVOList);
        }
        //3、如果redis中没有数据，则从mysql中获取
        DishPageDTO dishPageDTO = DishPageDTO.builder().categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        dishVOList = dishService.getParamList(dishPageDTO);
        log.info("从mysql服务器中获取的数据:{}",dishVOList);

        //4、把mysql中获取的集合保存到redis中
        valueOperations.set("DISH:"+categoryId,dishVOList);

        return Result.success(dishVOList); //营业中
    }


}
